<?php
include_once(DB_FILE);

/** funzione parserSimulazioni().\n
* Legge il file risultati_xxxx_xx_xx_xx.txt che riporta i rusultati prevista
* carica i dati in DB table 
* @param  void
* @return 0 
*/

function deleteOldDati()
{
    
    //$log=new FileLog();
    //$log->writeLog( __FUNCTION__."$filename");
    //$log->close();
   
    $msql  = new Db;
    $msql->connect();
    $msql->execute("DELETE from simulazioni;"); //eliminare i vecchi valori
    $msql->execute("DELETE from flag_simulazioni;");
    $msql->execute("DELETE from rasponi_tmp;");
    $msql->disconnettiDB();
}

function parserSimulazioni()
{
	global $DATA_SCHEDULE_SIMUL;

        
	foreach (glob(SIMULAZIONI_DIR."/risultati*") as $filename) {
	    echo "$filename size " . filesize($filename) . "\n";
	}
        echo "File: ".SIMULAZIONI_DIR."$filename";

	$log=new FileLog();
	$log->writeLog(__FUNCTION__."$filename size " . filesize($filename));
	$log->close();
	
	$msql  = new Db;
	$msql->connect();
//        $arr_data=$msql->execute_select("SELECT data FROM dati_impianto order  by data desc limit 1;");
//        $data=strtotime( $arr_data[0][0] );
        
	//$data=time();
        $data=strtotime( $DATA_SCHEDULE_SIMUL );

        $ins_query="INSERT INTO simulazioni (data_pre, ora_pre, P1Pz, P2Pz, P3Pz, P4Pz, P5Pz, P6Pz, P7Pz, P8Pz, P9Pz, P10Pz, P11Pz, P12Pz, P13Pz, P14Pz, P15Pz, P16Pz, P17Pz, P18Pz, P19Pz, P20Pz, P21Pz, P22Pz, P23Pz, P24Pz, P25Pz, P26Pz, P27Pz, P28Pz, P29Pz, P30Pz, P31Pz, P32Pz, P33Pz, P34Pz, P35Pz, P36Pz, P37Pz, data_insert) VALUES (";
	 
	$lines = file($filename); 
	$num_row=count($lines);

	for($i=0;$i<$num_row;$i++)
	{ 
		 $query="";
		 $string = $lines[$i];
		 $string= preg_replace(array('/\s\s+/','/ /'), ' ', $string);
		 $arr=preg_split('/ /',$string);
		
		 for($x=0;$x<count($arr);$x++)
		 {
		 	if($arr[$x]!='')
			{
				if( ($arr[0]== '' && $x == 1)||
				    ($arr[0]!= '' && $x == 0) )
				{
					//Primo valore un intero che rappresenta la data
					//bisogna traslare la data 
					$data_sec=$data+ 3600 * $arr[$x];
					$data_db=strftime('%Y-%m-%d %H:%M:%S',$data_sec);
					
                                        $query.="'".trim($data_db)."',";
                                        //echo $query."\n";
					//$arr[$x]=trim($data_db);
				}
				$query.="'".$arr[$x]."'";
		
				if($x!=count($arr)-1)
					$query.=",";
				else
					$query.=",'".$DATA_SCHEDULE_SIMUL."');";
			}
		 }
		 $query=$ins_query.$query;
		 //echo $query."\n";
		 $msql->execute($query);
	}
	$msql->disconnettiDB();
        unlink($filename);
	return 0;
}

/** funzione parserSimulazioni().\n
* Legge il file simulazioni.txt che riporta i rusultati prevista
* carica i dati in DB table 
* @param  void
* @return 0 
*/
function parserFlagSimulazioni()
{
	foreach (glob(SIMULAZIONI_DIR."/simulazioni.txt") as $filename) {
	    echo "$filename size " . filesize($filename) . "\n";
	}
	
	$log=new FileLog();
	$log->writeLog(__FUNCTION__."$filename size " . filesize($filename));
	$log->close();
	
	$msql  = new Db;
	$msql->connect();
	$ins_query="INSERT INTO flag_simulazioni (numero,flag) VALUES (";
	 
	$lines = file($filename); 
	$num_row=count($lines);
	
	for($i=0;$i<$num_row;$i++)
	{ 
		 $query=$ins_query;
		 $string = $lines[$i];
		 $string= preg_replace(array('/\s\s+/','/ /'), '', $string);
		 $query.=($i+1).",".$string.");";
		 echo $query."\n";
		 $msql->execute($query);
	}
	$msql->disconnettiDB();
	return 0;
}
?>
